﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Kokomo.Mirror.CliMetadata.Signatures;

namespace Kokomo.Mirror.CliMetadata
{
	class CliLocalInfo : LocalVariableInfo
	{
		public CliLocalInfo(
			VariableSignature signature,
			MethodBase declaringMethod,
			int index,
			bool isPinned
			)
		{
			if (signature == null) throw new ArgumentNullException("signature");
			if ((object)declaringMethod == null) throw new ArgumentNullException("declaringMethod");

			this._signature = signature;
			this._declaringMethod = declaringMethod;
			this._index = index;
			this._isPinned = isPinned;
		}

		private VariableSignature _signature;
		private MethodBase _declaringMethod;

		private Type _variableType;
		public override Type LocalType
		{
			get
			{
				Type[] genericMethodArgs = this._declaringMethod.GetGenericArguments();
				Type[] genericTypeArgs = this._declaringMethod.DeclaringType.GetGenericArguments();

				Type variableType = this._signature.ResolveType(
					this._declaringMethod.Module,
					true,
					genericTypeArgs,
					genericMethodArgs
					);
				return variableType;
			}
		}

		private int _index;
		public override int LocalIndex
		{
			get { return this._index; }
		}

		private bool _isPinned;
		public override bool IsPinned
		{
			get { return this._signature.IsPinned; }
		}
	}
}
